//
//  MJHistoryController.m
//  liulanqi
//
//  Created by HappyMilk 02 on 2017/1/7.
//  Copyright © 2017年 HappyMilk 02. All rights reserved.
//

#import "MJHistoryController.h"
#import "MJHistoryCell.h"

#import "MJLocaStorage.h"

#define HISTORY_URL @"historyurl"
#define COLLECTE_URL @"collecturl"


@interface MJHistoryController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tabelview;
@property (weak, nonatomic) IBOutlet UILabel *vc_title;

@property (nonatomic ,strong) NSMutableArray *listArray;

@end

@implementation MJHistoryController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (self.type) {
        self.vc_title.text = @"历史记录";
        [self getHistoryData];
    }else{
        self.vc_title.text = @"我的收藏";
        [self getCollectionData];
    }
    
    

}
- (void)getCollectionData{
    _listArray = [[MJLocaStorage getArrayFile:COLLECTE_URL] mutableCopy];
    [self.tabelview reloadData];
}

- (void)getHistoryData{
    _listArray = [[MJLocaStorage getArrayFile:HISTORY_URL] mutableCopy];
    [self.tabelview reloadData];
}

- (IBAction)popvc:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)clearnHistory:(id)sender {
    
    if (self.type) {
        [MJLocaStorage removeFileByFileName:HISTORY_URL];
        [self getHistoryData];
    }else{
        [MJLocaStorage removeFileByFileName:COLLECTE_URL];
        [self getCollectionData];
    }
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSDictionary *dic = _listArray[_listArray.count-indexPath.row-1];
    static NSString *cellid = @"MJHistoryCell";
    MJHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"MJHistoryCell" owner:nil options:nil].lastObject;
    }
    //NSLog(@"%@%@",dic[@"title"],dic[@"url"]);
    cell.title.text = dic[@"title"];
    cell.urlstring.text = dic[@"url"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dic = _listArray[_listArray.count-indexPath.row-1];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"dianji" object:nil userInfo:@{@"urlstr":dic[@"url"]}];
    [self popvc:nil];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
